#ifndef _SM2_H_
#define _SM2_H_
/*******************************************************************************
**                      Includes                                              **
*******************************************************************************/
#include "typedef.h"
#include "big.h"
#include "sm3.h"

#define    DEBUG_TESTCODE     1U
#define    USER_TESTCASE      6U

#define ECC_NUMWORD 32

#define ECC_NUMBITS 32 * 8

#define SM2_CRYPTPUBKEY_DEFAULTLEN   64u
#define SM2_CRYPTPRIKEY_DEFAULTLEN   32u
#define SM2_C1_DEFAULTLEN   64u
#define SM2_C3_DEFAULTLEN   32u
typedef uint64 sm2_bn_t[8];

// SM2 P
extern sm2_bn_t GM_BN_P;
// SM2 N
extern sm2_bn_t GM_BN_N;
// SM2 è’™å“¥é�??åˆ©åŸŸ G
extern sm2_point_t * GM_MONT_G;

// è’™å“¥é�??åˆ©åŸŸ, SM2 A
extern sm2_bn_t GM_BN_MONT_A;

// è’™å“¥é�??åˆ©åŸŸ, SM2 B
extern sm2_bn_t GM_BN_MONT_B;

extern sm2_bn_t GM_BN_MONT_NONE;


typedef struct {
    sm2_bn_t private_key;            // SM2私钥
    sm2_point_t public_key;          // SM2�?�?

    unsigned char x2y2[64];         // SM2 crypt x2y2
    sm3_ctx sm3_ctxset;         // SM3上下文，用于计算加解密C3
    unsigned char buf[32];          // 缓冲区，32字节为一�?数据�?
    unsigned int cur_buf_len;       // 当前缓冲区长�?
    unsigned int ct;                // SM2 crypt ct，�?�算KDF
    unsigned int state;             // 标识�?否为加密或是否为签名
} sm2_ctx;


/************************************************************************************************************
 *  Cry_SM2_SignMsg()
 ***********************************************************************************************************/
/*!
 * \brief         Signs a message using the SM2 signature scheme.
 * \details       This function implements the SM2 signature generation process, including:
 *                1. Calculation of ZA (user-specific hash based on ID and public key).
 *                2. Message hash generation using SM3.
 *                3. Computation of the signature components \( r \) and \( s \).
 *                4. Verification of the generated values to ensure validity.
 *
 * \param[in]     PrivateKey   Pointer to the 32-byte private key.
 * \param[in]     PublicKey    Pointer to the 64-byte public key (x and y each 32 bytes).
 * \param[in]     Message      Pointer to the message buffer.
 * \param[in]     MessageLen   Length of the message in bytes.
 * \param[in]     User_Id      Pointer to the user ID buffer.
 * \param[in]     User_Len     Length of the user ID in bytes.
 * \param[out]    SignatureR   Pointer to the buffer for the r component of the signature.
 * \param[out]    SignatureS   Pointer to the buffer for the s component of the signature.
 *
 * \return        Std_ReturnType  E_OK if the signature generation is successful, E_NOT_OK otherwise.
 **********************************************************************************************************/
extern Std_ReturnType Cry_SM2_SignMsg(uint8 *PrivateKey, uint8 *PublicKey,const uint8 *Message, uint32 MessageLen, const uint8 *User_Id, uint32 User_Len, uint8 *SignatureR, uint8 *SignatureS);


extern Std_ReturnType Cry_SM2_VerifyMsg(uint8 *PublicKey,const uint8 *Message, uint32 MessageLen, const uint8 *User_Id, uint32 User_Len, uint8 *SignatureR, uint8 *SignatureS);

extern Std_ReturnType Cry_SM2Encrypt(uint8 *PublicKey, uint8 *message, uint32 messageLen, uint8 *output);
extern Std_ReturnType Cry_SM2Decrypt(uint8 *PrivateKey, uint8 *ciphermessage, uint32 ciphermessageLen, uint8 *output);
#endif /* _SM2_H_ */
